<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="菜单名称" prop="menuName">
        <el-input
          v-model="queryParams.menuName"
          placeholder="请输入菜单名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="食堂编号" prop="canteenId">
        <el-input
          v-model="queryParams.canteenId"
          placeholder="请输入食堂编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="菜单状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="菜单状态" clearable size="small">
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <div v-show="isHelp==false">
        <el-col :span="1.5">
          <el-button
            type="primary"
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['fb:menu:add']"
          >新增</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="success"
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['fb:menu:edit']"
          >修改</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['fb:menu:remove']"
          >删除</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="warning"
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            v-hasPermi="['fb:menu:export']"
          >导出</el-button>
        </el-col>
      </div>
      <div v-show="isHelp==true">
        <el-col :span="1.5">
          <el-button
            type="success"
            icon="el-icon-check"
            size="mini"
            :disabled="multiple"
            @click="checkData"
          >确认</el-button>
        </el-col>
      </div>  
	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="menuList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="菜单编号" width="80" prop="menuId" />
      <el-table-column label="菜单名称" align="center" prop="menuName" />
      <el-table-column prop="menuType" align="center" label="菜单类型" :formatter="menuTypeFormat" width="80"></el-table-column>
      <el-table-column label="菜单价格" align="center" prop="menuPrice" />
      <!-- <el-table-column label="食堂编号" align="center" prop="canteenId" /> 
      <el-table-column label="显示顺序" align="center" prop="orderNum" /> -->
      <el-table-column prop="status" align="center"  label="菜单状态" :formatter="statusFormat" width="80"></el-table-column>
      <el-table-column prop="menuImg"
        header-align="center"
        align="center"
        label="菜图图片">
        <template width="40" slot-scope="scope">
          <el-image :src="scope.row.menuImg" style="width: 100px; height: 100px"></el-image>
        </template>
      </el-table-column>
      <el-table-column  v-if="isHelp==false" label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['fb:menu:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['fb:menu:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改食堂菜单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <!-- <el-form-item label="食堂编号" prop="canteenId">
          <el-input v-model="form.canteenId" placeholder="请输入食堂编号" />
        </el-form-item> -->
        <el-form-item label="菜单名称" prop="menuName">
          <el-input v-model="form.menuName" placeholder="请输入菜单名称" />
        </el-form-item>
        <el-form-item label="菜单类型" prop="menuType">
          <el-radio-group v-model="form.menuType">
            <el-radio
                v-for="dict in menuTypeOptions"
                :key="dict.dictValue"
                :label="dict.dictValue"
              >{{dict.dictLabel}}</el-radio>
            </el-radio-group>
        </el-form-item>
        <el-form-item label="菜单价格" prop="menuPrice">
          
          <el-input-number v-model="form.menuPrice" placeholder="请输入菜单价格" />
        </el-form-item>
        <!-- <el-form-item label="显示状态" prop="visible">
          <el-radio-group v-model="form.visible">
            <el-radio
              v-for="dict in visibleOptions"
              :key="dict.dictValue"
              :label="dict.dictValue"
            >{{dict.dictLabel}}</el-radio>
          </el-radio-group>
        </el-form-item> -->
        <el-form-item label="显示顺序" prop="orderNum">
          <el-input-number v-model="form.orderNum" controls-position="right" :min="0" />
        </el-form-item>
        <el-form-item label="菜单状态">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in statusOptions"
              :key="dict.dictValue"
              :label="dict.dictValue"
            >{{dict.dictLabel}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="菜单图片">
          <uploadImage v-model="form.menuImg"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listMenu, getMenu, delMenu, addMenu, updateMenu, exportMenu } from "@/api/fb/menu";
import UploadImage from '@/components/UploadImage';
export default {
  name: "Menu",
  components: {
    UploadImage,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 食堂菜单表格数据
      menuList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 显示状态数据字典
      visibleOptions: [],
      // 菜单状态数据字典
      statusOptions: [],
      //食堂菜单类型字典
      menuTypeOptions: [],
      //选中对象
      multipleSelection: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        menuName: null,
        menuType: null,
        menuImg: null,
        orderNum: null,
        menuPrice: null,
        visible: null,
        status: null,
        canteenId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        menuName: [
          { required: true, message: "菜单名称不能为空", trigger: "blur" }
        ],
        menuPrice: [
          { required: true, message: "菜单价格不能为空", trigger: "blur" }
        ],
        menuType: [
          { required: true, message: "菜单类型不能为空", trigger: "blur" }
        ]
        // canteenId: [
        //   { required: true, message: "食堂编号不能为空", trigger: "blur" }
        // ]
      }
    };
  },
  props: {
    isHelp: {type: Boolean,default: false}
  },
  created() {
    this.getList();
    // this.getDicts("sys_show_hide").then(response => {
    //   this.visibleOptions = response.data;
    // });
    this.getDicts("sys_normal_disable").then(response => {
      this.statusOptions = response.data;
    });
    this.getDicts("fb_menu_type").then(response => {
      this.menuTypeOptions = response.data;
    });
  },
  methods: {
    /** 查询食堂菜单列表 */
    getList() {
      this.loading = true;
      listMenu(this.queryParams).then(response => {
        this.menuList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    //菜单类型字段翻译
    menuTypeFormat(row, column) {
      return this.selectDictLabel(this.menuTypeOptions, row.menuType);
    },
    // 显示状态字典翻译
    visibleFormat(row, column) {
      return this.selectDictLabel(this.visibleOptions, row.visible);
    },
    // 菜单状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        menuId: null,
        menuName: null,
        menuType: null,
        menuImg: null,
        orderNum: null,
        menuPrice: null,
        visible: null,
        status: "0",
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        canteenId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.menuId)
      this.single = selection.length!==0
      this.multiple = !selection.length
      this.multipleSelection = selection.length!==0?selection:[];
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加食堂菜单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const menuId = row.menuId || this.ids
      getMenu(menuId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改食堂菜单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.menuId != null) {
            updateMenu(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMenu(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const menuIds = row.menuId || this.ids;
      this.$confirm('是否确认删除食堂菜单编号为"' + menuIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delMenu(menuIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有食堂菜单数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportMenu(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    },
    /** 通用帮助选择确认按钮 */
    checkData() {
      this.$emit('addMenu', this.multipleSelection);
    }
  }
};
</script>
